/**
 * 
 */
package dms.service.imp;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import dms.dto.ClassesDTO;
import dms.dto.FacultyDTO;
import dms.dto.SchoolDTO;
import dms.model.Classes;
import dms.model.Faculty;
import dms.model.School;
import dms.model.util.PageIterator;
import dms.repository.SchoolRepository;
import dms.service.SchoolService;

/**
 * @author 李健
 *
 */
public class SchoolServiceImpl implements SchoolService {

	private static final Log log = LogFactory.getLog(SchoolServiceImpl.class);
	
	/**
	 * @param schoolRepository
	 */
	public SchoolServiceImpl(SchoolRepository schoolRepository) {
		super();
		this.schoolRepository = schoolRepository;
	}

	private SchoolRepository schoolRepository = null;
	
	/**
	 * @return the schoolRepository
	 */
	public SchoolRepository getSchoolRepository() {
		return schoolRepository;
	}

	/**
	 * @param schoolRepository the schoolRepository to set
	 */
	public void setSchoolRepository(SchoolRepository schoolRepository) {
		this.schoolRepository = schoolRepository;
	}

	@Override
	public Classes createClasses(ClassesDTO classesInfo) {
		Classes classes = new Classes();
		Faculty faculty = this.schoolRepository.findFacultyById(classesInfo.getFaculty());
				
		classes.setName(classesInfo.getName());
		
		classes.setFaculty(faculty);
		return this.schoolRepository.createClasses(classes);
		
	}

	@Override
	public Faculty createFaculty(FacultyDTO facultyInfo) {
		Faculty faculty = new Faculty();
		School school = this.schoolRepository.findSchoolById(facultyInfo.getSchool());
		
		faculty.setDescription(facultyInfo.getDescription());
		faculty.setName(facultyInfo.getName());
		
		faculty.setSchool(school);
		return this.schoolRepository.createFaculty(faculty);
		
	}

	@Override
	public School createSchool(SchoolDTO schoolInfo) {
		School school = new School();
		
		try {
			BeanUtils.copyProperties(school, schoolInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
	
		return this.schoolRepository.createSchool(school);
	}

	@Override
	public void deleteClasses(List<Long> ids) {
		this.schoolRepository.deleteClasses(ids);
		
	}

	@Override
	public void deleteClassesById(Long id) {
		this.schoolRepository.deleteClassesById(id);
		
	}

	@Override
	public void deleteFaculties(List<Long> ids) {
		this.schoolRepository.deleteFaculites(ids);
		
	}

	@Override
	public void deleteFacultyById(Long id) {
		this.schoolRepository.deleteFacultyById(id);
		
	}

	@Override
	public void deleteSchoolById(Long id) {
		this.schoolRepository.deleteSchoolById(id);
		
	}

	@Override
	public void deleteSchools(List<Long> ids) {
		this.schoolRepository.deleteSchools(ids);
		
	}

	@Override
	public PageIterator<School> findAllSchool(int start, int count) {
		return this.schoolRepository.findAllSchool(start, count);
	}

	@Override
	public PageIterator<Classes> findClassesByFaculty(Faculty faculty,
			int start, int count) {
		return this.schoolRepository.findClassesByFaculty(faculty, start, count);
	}

	@Override
	public Classes findClassesById(Long id) {
		return this.schoolRepository.findClassesById(id);
	}

	@Override
	public PageIterator<Classes> findClassesByName(String name, int start,
			int count) {
		return this.schoolRepository.findClassesByName(name, start, count);
	}

	@Override
	public Faculty findFacultyById(Long id) {
		return this.schoolRepository.findFacultyById(id);
	}

	@Override
	public PageIterator<Faculty> findFacultyBySchool(School school, int start,
			int count) {
		return this.schoolRepository.findFacultyBySchool(school, start, count);
	}

	@Override
	public School findSchoolById(Long id) {
		return this.schoolRepository.findSchoolById(id);
	}

	@Override
	public School findSchoolByName(String name, int start, int count) {
		PageIterator<School> iter = this.schoolRepository.findSchoolByName(name, start, count);
		
		School school = null;
		while (iter.hasNext()) {
			school = iter.next();
		}
		
		return school;
	}

	@Override
	public Classes updateClasses(ClassesDTO classesInfo) {
		
		Classes classes = this.schoolRepository.findClassesById(classesInfo.getId());
		Faculty faculty = this.schoolRepository.findFacultyById(classesInfo.getFaculty());
				
	/*	try {
			BeanUtils.copyProperties(classes, classesInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}*/
		classes.setName(classesInfo.getName());
		
		classes.setFaculty(faculty);
		return this.schoolRepository.updateClasses(classes);
	}

	@Override
	public Faculty updateFaculty(FacultyDTO facultyInfo) {
		Faculty faculty = this.schoolRepository.findFacultyById(facultyInfo.getId());
		School school = this.schoolRepository.findSchoolById(facultyInfo.getSchool());
		
		/*try {
			BeanUtils.copyProperties(faculty, facultyInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}*/
		faculty.setDescription(facultyInfo.getDescription());
		faculty.setName(facultyInfo.getName());
		
		faculty.setSchool(school);
		return this.schoolRepository.updateFaculty(faculty);
	}

	@Override
	public School updateSchool(SchoolDTO schoolInfo) {
	School school = this.schoolRepository.findSchoolById(schoolInfo.getId());
		
		try {
			BeanUtils.copyProperties(school, schoolInfo);
		} catch (IllegalAccessException e) {
			log.error(e.getMessage());
		} catch (InvocationTargetException e) {
			log.error(e.getMessage());
		}
	
		return this.schoolRepository.updateSchool(school);
	}

	/**
	 * 
	 */
	public SchoolServiceImpl() {
		super();
	}

}
